package com.molyfun.parents.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.account.LoginActivity;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.adapter.SmartFragmentStatePagerAdapter;
import com.molyfun.parents.fragment.GuideImageFragment;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/27/16.
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.enter)
    TextView mEnter;

//    @InjectView(R.id.guide_dots)
//    LinearLayout mDotsLayout;
//    @InjectView(R.id.dot_first)
//    ImageButton dotFirstImageButton;
//    @InjectView(R.id.dot_second)
//    ImageButton dotSecondImageButton;
//    @InjectView(R.id.dot_third)
//    ImageButton dotThirdImageButton;
//    @InjectView(R.id.dot_four)
//    ImageButton dotFourImageButton;
//    @InjectView(R.id.dot_five)
//    ImageButton dotFiveImageButton;

    @InjectView(R.id.guide_viewpager)
    ViewPager viewPager;

    GuidePagerAdapter adapterViewPager;
    List<Fragment> fragmentList;

//    private ArgbEvaluator mArgbEvaluator;

//    private int colorBg[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        ButterKnife.inject(this);
//        dotFirstImageButton.setSelected(true);

        PreferencesStorageUtil.setOldUser(this);

        fragmentList = new ArrayList<>();

        fragmentList.add(GuideImageFragment.newInstance(R.mipmap.guide_img1, R.color.guide_color_1));
        fragmentList.add(GuideImageFragment.newInstance(R.mipmap.guide_img2, R.color.guide_color_2));
        fragmentList.add(GuideImageFragment.newInstance(R.mipmap.guide_img3, R.color.guide_color_3));
        fragmentList.add(GuideImageFragment.newInstance(R.mipmap.guide_img4, R.color.guide_color_4));
        fragmentList.add(GuideImageFragment.newInstance(R.mipmap.guide_img5, R.color.guide_color_5));

        adapterViewPager = new GuidePagerAdapter(getSupportFragmentManager(), fragmentList);

//        mArgbEvaluator = new ArgbEvaluator();

//        colorBg = getResources().getIntArray(R.array.splash_bg);

        mEnter.setOnClickListener(this);

        viewPager.setAdapter(adapterViewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                int color = (int)mArgbEvaluator.evaluate(positionOffset, colorBg[position % colorBg.length],
//                        colorBg[(position + 1) % colorBg.length]);
//                System.out.println("color--->" + color);
//                getWindow().setBackgroundDrawable(new ColorDrawable(color));
            }

            @Override
            public void onPageSelected(int newPosition) {
                mEnter.setVisibility(View.INVISIBLE);

                switch (newPosition) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        mEnter.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.enter:
                Intent intent = null;
                if(TextUtils.isEmpty(PreferencesStorageUtil.getToken(GuideActivity.this))){
                    intent = new Intent(GuideActivity.this, LoginActivity.class);
                }else{
                    intent = new Intent(GuideActivity.this, MainActivity.class);
                }
                startActivity(intent);
                finish();
                break;
        }
    }

    public static class GuidePagerAdapter extends SmartFragmentStatePagerAdapter {

        List<Fragment> fragmentList;

        public GuidePagerAdapter(android.support.v4.app.FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            this.fragmentList = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

}
